package com.maimang.platform.quartz.utils;

import com.maimang.platform.common.utils.bean.BeanUtils;
import com.maimang.platform.common.utils.spring.SpringUtils;
import com.maimang.platform.constant.Constants;
import com.maimang.platform.constant.ScheduleConstants;
import com.maimang.platform.lang.StringUtils;
import com.maimang.platform.quartz.entity.SysJob;
import com.maimang.platform.quartz.entity.SysJobLog;
import com.maimang.platform.quartz.service.SysJobLogService;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;
import java.util.concurrent.Future;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname ScheduleJob
 * @description 定时任务处理(禁止并发执行多个相同定义的JobDetail,使用DisallowConcurrentExecution注解)
 * @date 2019-04-16 15:52
 * @email jintaozhao@qq.com
 */
@DisallowConcurrentExecution
public class ScheduleJob extends QuartzJobBean {
	private static final Logger log = LoggerFactory.getLogger(ScheduleJob.class);
	private final static SysJobLogService jobLogService = SpringUtils.getBean(SysJobLogService.class);
	//    private ExecutorService service = Executors.newSingleThreadExecutor();
	private ThreadPoolTaskExecutor executor = SpringUtils.getBean("threadPoolTaskExecutor");

	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		SysJob job = new SysJob();
		BeanUtils.copyBeanProp(job, context.getMergedJobDataMap().get(ScheduleConstants.TASK_PROPERTIES));

		SysJobLog jobLog = new SysJobLog();
		jobLog.setJobName(job.getJobName());
		jobLog.setJobGroup(job.getJobGroup());
		jobLog.setMethodName(job.getMethodName());
		jobLog.setMethodParams(job.getMethodParams());
		jobLog.setCreateTime(new Date());

		long startTime = System.currentTimeMillis();

		try {
			// 执行任务
			log.info("任务开始执行 - 名称：{} 方法：{}", job.getJobName(), job.getMethodName());
			ScheduleRunnable task = new ScheduleRunnable(job.getJobName(), job.getMethodName(), job.getMethodParams());
			Future<?> future = executor.submit(task);
			future.get();
			long times = System.currentTimeMillis() - startTime;
			// 任务状态 0：成功 1：失败
			jobLog.setStatus(Constants.SUCCESS);
			jobLog.setJobMessage(job.getJobName() + " 总共耗时：" + times + "毫秒");

			log.info("任务执行结束 - 名称：{} 耗时：{} 毫秒", job.getJobName(), times);
		} catch (Exception e) {
			log.info("任务执行失败 - 名称：{} 方法：{}", job.getJobName(), job.getMethodName());
			log.error("任务执行异常  - ：", e);
			long times = System.currentTimeMillis() - startTime;
			jobLog.setJobMessage(job.getJobName() + " 总共耗时：" + times + "毫秒");
			// 任务状态 0：成功 1：失败
			jobLog.setStatus(Constants.FAIL);
//            jobLog.setExceptionInfo(e.toString());
			jobLog.setExceptionInfo(StringUtils.substring(e.getMessage(), 0, 2000));
		} finally {
			jobLogService.addJobLog(jobLog);
		}
	}
}
